package cn.timeapi.Applet.Mall.CatSquare.controller.portal;


import cn.timeapi.Applet.Mall.CatSquare.common.*;
import cn.timeapi.Applet.Mall.CatSquare.entity.Account;
import cn.timeapi.Applet.Mall.CatSquare.entity.Account;
import cn.timeapi.Applet.Mall.CatSquare.service.ICartService;
import cn.timeapi.Applet.Mall.CatSquare.service.ICategoryService;
import cn.timeapi.Applet.Mall.CatSquare.vo.CartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 描述:购物车模块
 * @author Vincent Vic
 * create 2020-02-20 10:07
 */

@Api("cart")
@RestController
@RequestMapping("/cart/")   //请求地址设置
public class CartController {

    @Autowired
    ICartService iCartService;

    @Autowired
    private RedisOperator<String,Account> redis;

    //添加购物车
    @ApiOperation("添加购物车")
    @GetMapping("add")
    @ResponseBody   //自动序列化Json
    public ServerResponse<CartVo> add(@ApiParam("会话码") @ParamsNotNull String sessionKey, @ParamsNotNull @ApiParam("") Integer count, @ParamsNotNull @ApiParam("") Integer productId){
        //获取当前用户
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }

        return iCartService.add(account.getOpenId(),productId,count);
    }

    //查询购物车
    @ApiOperation("查询购物车")
    @GetMapping("list")
    @ResponseBody   //自动序列化Json
    public ServerResponse<CartVo> list(@ApiParam("会话码") @ParamsNotNull String sessionKey){
        //获取当前用户
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }
        return iCartService.list(account.getOpenId());
    }

    //更新购物车
    @ApiOperation("更新购物车")
    @GetMapping("update")
    @ResponseBody   //自动序列化Json
    public ServerResponse<CartVo> update(@ApiParam("会话码") @ParamsNotNull String sessionKey, @ParamsNotNull @ApiParam("") Integer count,@ParamsNotNull @ApiParam("") Integer productId){
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }

        return iCartService.updata(account.getOpenId(),productId,count);
    }

    //删除购物车商品
    //productId 通过逗号分隔，删除多个商品
    @ApiOperation("删除购物车商品  productId 通过逗号分隔，删除多个商品")
    @GetMapping("detele_product")
    @ResponseBody   //自动序列化Json
    public ServerResponse<CartVo> deteleProduct(@ApiParam("会话码") @ParamsNotNull String sessionKey, @ApiParam("") String productIds){
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }
        return iCartService.deleteProduct(account.getOpenId(),productIds);
    }

    //购物车全选
    @ApiOperation("购物车全选")
    @GetMapping("select_all")
    @ResponseBody   //自动序列化Json
    public ServerResponse<CartVo> selectAll(@ApiParam("会话码") @ParamsNotNull String sessionKey){
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }
        return iCartService.selectOrUnselect(account.getOpenId(),null,Const.Cart.CHECKED);
    }
    //购物车取消全选
    @ApiOperation("购物车取消全选")
    @GetMapping("un_select_all")
    @ResponseBody   //自动序列化Json
    public ServerResponse<CartVo> unSelectAll(@ApiParam("会话码") @ParamsNotNull String sessionKey){
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }
        return iCartService.selectOrUnselect(account.getOpenId(),null,Const.Cart.UN_CHECKED);
    }
    //购物车商品选择
    @ApiOperation("购物车商品选择")
    @GetMapping("select")
    @ResponseBody   //自动序列化Json
    public ServerResponse<CartVo> Select(@ApiParam("会话码") @ParamsNotNull String sessionKey,@ParamsNotNull @ApiParam("") Integer productId){
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }
        return iCartService.selectOrUnselect(account.getOpenId(),productId,Const.Cart.CHECKED);
    }
    //购物车产品取消选择
    @ApiOperation("购物车产品取消选择")
    @GetMapping("un_select")
    @ResponseBody   //自动序列化Json
    public ServerResponse<CartVo> unSelect(@ApiParam("会话码") @ParamsNotNull String sessionKey,@ParamsNotNull @ApiParam("") Integer productId){
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }
        return iCartService.selectOrUnselect(account.getOpenId(),productId,Const.Cart.UN_CHECKED);
    }
    //查询购物车商品数量
    @ApiOperation("查询购物车商品数量")
    @GetMapping("get_cart_product_count")
    @ResponseBody   //自动序列化Json
    public ServerResponse<Integer> getCartProductCount(@ApiParam("会话码") @ParamsNotNull String sessionKey){
        //判断是否登入
		Account account = redis.get(Const.REDIS_LOGIN_PREFIX + sessionKey);
        if (account == null){
            return ServerResponse.success();
        }
        return iCartService.getCartProductCount(account.getOpenId());
    }

}